package com.jml.springsecurity.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.jml.springsecurity.dto.User;
import com.jml.springsecurity.dto.User.UserDetailView;
import com.jml.springsecurity.dto.User.UserSimpleView;
import com.jml.springsecurity.dto.UserQueryCondition;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.function.Consumer;
import org.springframework.data.domain.Pageable;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;


@RestController
@RequestMapping("/user")
public class UserController {

	@RequestMapping(value = "/user",method = RequestMethod.GET)
	@GetMapping
	@JsonView(UserSimpleView.class) //返回结果只有UserSimpleView中注解的字段
	public List<User> query(UserQueryCondition condition,@PageableDefault(page = 2,size = 7,sort = "username.desc") Pageable pageable){
		//required = false表示不是必填，默认是必填
		//name = "userN"表示http传过来的参数名字一定要是userN，否则接收不到
		System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE));

		System.out.println(pageable.getPageSize());
		System.out.println(pageable.getPageNumber());
		System.out.println(pageable.getSort());

		List<User> users = new ArrayList<>();
		users.add(new User());
		users.add(new User());
		users.add(new User());
		return users;
	}

	//表示传入参数id，\\d+是正则，表明传入的是数字
	//@RequestMapping(value = "/user/{id:\\d+}")
	@GetMapping("/{id:\\d+}")
	@JsonView(UserDetailView.class)
	public User getInfo(@PathVariable(value = "id") String id) {
		System.out.println("进入getInfo服务");
		User user = new User();
		user.setUsername("tom");
		return user;
	}

	//@Valid 校验类属性
	//BindingResult是和注解@Valid一起的，
	//如果不符合@Valid中规定的就会把错误信息放入BindResult
	@PostMapping
	public User create(@Valid @RequestBody User user,BindingResult erros) {
		if(erros.hasErrors()){
			erros.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
		}
		System.out.println(user.getId());
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
		System.out.println(user.getBirthday());
		user.setId("1");
		return user;
	}


	@PutMapping("/{id:\\d+}")
	public User update(@Valid @RequestBody User user, BindingResult erros) {
		if(erros.hasErrors()){
			erros.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
		}
		System.out.println(user.getId());
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
		System.out.println(user.getBirthday());
		user.setId("1");
		return user;
	}

	@DeleteMapping("/{id:\\d+}")
	public void delete(@PathVariable String id) {
		System.out.println(id);
	}



}
